package com.example.gokuplayalong.bases.mobsdk;

import android.os.Message;

import com.example.gokuplayalong.bases.beans.AppParams;
import com.example.gokuplayalong.bases.exception.ExceptionUtils;
import com.example.gokuplayalong.bases.tools.CaptchaTimer;
import com.example.gokuplayalong.service.home.HomeLogin;
import com.kongzue.dialogx.dialogs.PopTip;
import com.kongzue.dialogx.dialogs.WaitDialog;
import com.mob.MobSDK;

import java.util.Objects;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import cn.smssdk.wrapper.TokenVerifyResult;

public class MobSdkSmSdk {
    static EventHandler eh = null;

    public static void init() {
        MobSDK.submitPolicyGrantResult(true);
        registerEventHandler();
    }

    public static void registerEventHandler() {

        eh = new EventHandler() {
            @Override
            public void afterEvent(int event, int result, Object data) {
                // TODO 此处为子线程！不可直接处理UI线程！处理后续操作需传到主线程中操作！
                if (result == SMSSDK.RESULT_COMPLETE) {
                    //成功回调
                    if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {

                        try {
                            //提交短信、语音验证码成功
                            CaptchaTimer.verifyCaptchaSuccess = 1;

                            if (Objects.equals(CaptchaTimer.captchaView, "HomeLoginForPage2")) {
                                HomeLogin homeLogin = (HomeLogin) AppParams.getInstance().sysfragmentmanager.findFragmentByTag("HomeLogin");
                                Message msg = new Message();
                                msg.what = 2;
                                if (homeLogin != null) {
                                    homeLogin.handler.sendMessage(msg);
                                }
                            }
                            else if (Objects.equals(CaptchaTimer.captchaView, "HomeLoginForPage3")) {
                                HomeLogin homeLogin = (HomeLogin) AppParams.getInstance().sysfragmentmanager.findFragmentByTag("HomeLogin");
                                Message msg = new Message();
                                msg.what = 4;
                                if (homeLogin != null) {
                                    homeLogin.handler.sendMessage(msg);
                                }
                            }
//                            else if (Objects.equals(CaptchaTimer.captchaView, "UserMainProfilePayoutsBindAlipay")) {
//                                UserMainProfilePayoutsBindAlipay userMainProfilePayoutsBindAlipay = (UserMainProfilePayoutsBindAlipay) AppParams.getInstance().sysfragmentmanager.findFragmentByTag("UserMainProfilePayoutsBindAlipay");
//                                Message msg = new Message();
//                                msg.what = 2;
//                                if (userMainProfilePayoutsBindAlipay != null) {
//                                    userMainProfilePayoutsBindAlipay.handler.sendMessage(msg);
//                                }
//                            }
                        } catch (Throwable e) {
                            ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                        }

                    }
                    else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {

                        try {
                            CaptchaTimer.getCaptchaSuccess = 1;
                            //获取短信验证码成功
                            CaptchaTimer.getInstance().start();
                            if (Objects.equals(CaptchaTimer.captchaView, "HomeLoginForPage1")) {
                                HomeLogin homeLogin = (HomeLogin) AppParams.getInstance().sysfragmentmanager.findFragmentByTag("HomeLogin");
                                Message msg = new Message();
                                msg.what = 1;
                                if (homeLogin != null) {
                                    homeLogin.handler.sendMessage(msg);
                                }
                            }
                            else if (Objects.equals(CaptchaTimer.captchaView, "HomeLoginForPage3")) {
                                HomeLogin homeLogin = (HomeLogin) AppParams.getInstance().sysfragmentmanager.findFragmentByTag("HomeLogin");
                                Message msg = new Message();
                                msg.what = 3;
                                if (homeLogin != null) {
                                    homeLogin.handler.sendMessage(msg);
                                }
                            }
//                            else if (Objects.equals(CaptchaTimer.captchaView, "UserMainProfilePayoutsBindAlipay")) {
//                                UserMainProfilePayoutsBindAlipay userMainProfilePayoutsBindAlipay = (UserMainProfilePayoutsBindAlipay) AppParams.getInstance().sysfragmentmanager.findFragmentByTag("UserMainProfilePayoutsBindAlipay");
//                                Message msg = new Message();
//                                msg.what = 1;
//                                if (userMainProfilePayoutsBindAlipay != null) {
//                                    userMainProfilePayoutsBindAlipay.handler.sendMessage(msg);
//                                }
//                            }
                        } catch (Throwable e) {
                            ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                        }


                    }
                    else if (event == SMSSDK.EVENT_GET_VOICE_VERIFICATION_CODE) {
                        //获取语音验证码成功
                    }
                    else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {
                        //返回支持发送验证码的国家列表
                    }
                    else if (event == SMSSDK.EVENT_GET_VERIFY_TOKEN_CODE) {
                        //本机验证获取token成功
                        TokenVerifyResult tokenVerifyResult = (TokenVerifyResult) data;
                        //SMSSDK.login(phoneNum,tokenVerifyResult);
                    }
                    else if (event == SMSSDK.EVENT_VERIFY_LOGIN) {
                        //本机验证登陆成功
                    }
                }
                else {
                    CaptchaTimer.getCaptchaSuccess = 1;
                    CaptchaTimer.verifyCaptchaSuccess = 1;
                    PopTip.show(data.toString());
                    WaitDialog.dismiss();
                    ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), (Throwable) data);
                }
            }
        };
        SMSSDK.registerEventHandler(eh); //注册短信回调
    }

    public static void unregisterEventHandler() {
        if (eh != null) SMSSDK.unregisterEventHandler(eh);
    }
}
